package net.itbaiyang.pattern.email.bridge.bridgeMajor;

import net.itbaiyang.pattern.email.bridge.mailType.MailStencil;
import net.itbaiyang.pattern.factory.DefaultEmailFactory;
import net.itbaiyang.pattern.factory.MailMessageFactory;
import net.itbaiyang.pattern.model.EmailDetails;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 这个方法时通用的构建发送器的方法，具体行为交给MailSender去执行
 */
public class CommonSender {

//    @Resource
//    JavaMailSender mailSender;

    /**
     * 发送器里面存入工厂对象
     */
    protected MailMessageFactory factory = new DefaultEmailFactory();
    /**
     * 通过组合的方式来桥接邮件模型特征
     */
    private Map<String,Object> mailInfo = new HashMap<>();

    public CommonSender() {
    }

    public CommonSender(EmailDetails details) throws Exception{
        MailStencil mailStencil1 = this.createMailStencil(details);
        this.createEmail(details,mailStencil1);
    }

    public Map<String, Object> getMailInfo() {
        return mailInfo;
    }

    /**
     * 创建邮件MailStenci
     */
    private MailStencil createMailStencil(EmailDetails details) throws Exception {
        MailStencil mailStencil1 = factory.createMailStencil(details);
        mailInfo.put("mailStencil1",mailStencil1);
        return mailStencil1;
    }

    /**
     * 构建具体邮件对象
     */
    private void createEmail(EmailDetails details, MailStencil template) throws Exception {

        MimeMessageHelper mimeMessageHelper = factory.createEmail(details.getMailSender(), details.getFrom(), template);
        mailInfo.put("helper",mimeMessageHelper);
    }
}
